package parser;

import antlr.BaseAST;
import antlr.Token;
import antlr.collections.AST;
import collector.Configuration;

public class CodeReviewAST extends BaseAST {
    private static final long serialVersionUID = 7900427365736214089L;
	/**
     * This AST node was created from what token?
     */
    protected CodeReviewToken token = null;
    protected int astType = -1;

    public void initialize(int i, String s) {
        token = new CodeReviewToken(i, s);
    }

    public void initialize(AST ast) {
        token = ((CodeReviewAST) ast).token;
    }

    public void initialize(Token newToken) {
        this.token = (CodeReviewToken) newToken;
    }

    /**
     * @return token value or anonymous replacement
     */
    public String getText() {
        int type = astType; //token.getType();
        if (Configuration.getInstance().anonymizeType(type)) {
            return JavaRecognizer._tokenNames[type];
        }
        return token.getText();
    }

    public void setType(int type) {
        token.setType(type);
    }

    public void setText(String text) {
        token.setText(text);
    }

    public void setAstType(int astType) {
        this.astType = astType;
    }

    public int getType() {
        if (token != null) {
            return token.getType();
        }
        return -1;
    }

    public int getLine() {
        int line = 0;
        if (token != null) {
            line = token.getLine();
        }
        return line;
    }

    public int getStartIndex() {
        int index = 0;
        if (token != null) {
            index = token.getStartIndex();
        }
        return index;
    }

    public int getEndIndex() {
        int index = 0;
        if (token != null) {
            index = token.getEndIndex();
        }
        return index;
    }

    public void setLine(int line) {
        token.setLine(line);
    }

    public CodeReviewToken getToken() {
        return token;
    }

    public void getNodeInfo(TextLine tl) {
        int tid = token.getId();
        if (tid > -1) tl.addToken(tid);

    }

    public void getInfo(TextLine tl) {
        String ts = getAnonymizedText(tl);

        if (getFirstChild() != null) {
            if (!Configuration.getInstance().anonymizeType(astType)) {
                ts += ((CodeReviewAST) getFirstChild()).toStringList(tl);
            }
        }
        tl.addString(ts);
    }

    private String getAnonymizedText(final TextLine tl) {
        AST t = this;

        int tid = token.getId();
        if (tid > -1) tl.addToken(tid);

        if (t.getType() > JavaTokenTypes.TYPE_LOWER_BOUNDS) {
            return " " + toString();
        }
        return "";
    }

    public String toStringList(TextLine tl) {
        String ts = "";

        int tid = token.getId();
        if (tid > -1) tl.addToken(tid);

        if (getFirstChild() != null) ts += "(";

        //avoid imagination token
        if (getType() > JavaTokenTypes.TYPE_LOWER_BOUNDS) {
            ts += " " + toString();
        }

        if (getFirstChild() != null) {
            if (!Configuration.getInstance().anonymizeType(astType)) {
                ts += ((CodeReviewAST) getFirstChild()).toStringList(tl);
            }
        }

        if (getFirstChild() != null) ts += ")";

        if (getNextSibling() != null) {
            ts += ((CodeReviewAST) getNextSibling()).toStringList(tl);
        }

        return ts;
    }

/*    *//** Get the ith child from 0 *//*
    public GrammarAST getChild(int i) {
        int n = 0;
        AST t = getFirstChild();
        while ( t!=null ) {
            if ( n==i ) {
                return (GrammarAST)t;
            }
            n++;
            t = (GrammarAST)t.getNextSibling();
        }
        return null;
    }

    public GrammarAST getFirstChildWithType(int ttype) {
        AST t = getFirstChild();
        while ( t!=null ) {
            if ( t.getType()==ttype ) {
                return (GrammarAST)t;
            }
            t = (GrammarAST)t.getNextSibling();
        }
        return null;
    }

    public GrammarAST[] getChildrenAsArray() {
        AST t = getFirstChild();
        GrammarAST[] array = new GrammarAST[getNumberOfChildren()];
        int i = 0;
        while ( t!=null ) {
            array[i] = (GrammarAST)t;
            t = t.getNextSibling();
            i++;
        }
        return array;
    }*/

}